package com.acce.core.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

public class PropertiesUtil {
	private static Properties properties = new Properties();

	public static PropertiesUtil getInstance() {
		return Holder.instance;
	}

	public Properties getProperties(String cfgFilePath) {
		try {
			properties = new Properties();
			InputStream in = PropertiesUtil.class.getClassLoader()
					.getResourceAsStream(cfgFilePath);
			properties.load(in);
		} catch (FileNotFoundException e) {
			properties = null;
			e.printStackTrace();
		} catch (IOException e) {
			properties = null;
			e.printStackTrace();
		}
		return properties;
	}

	public String getPropertiesPath(String cfgFilePath) {
		String resourcePath = PropertiesUtil.class.getResource("").getPath();
		String webInfPath = resourcePath.substring(0,
				resourcePath.lastIndexOf("/classes")).toString();
		cfgFilePath = StringUtils.trimToEmpty(cfgFilePath);
		if (StringUtils.isNotBlank(cfgFilePath)) {
			return webInfPath.concat(cfgFilePath).toString();
		}

		return webInfPath;
	}

	public static String getConfigurationPath(String cfgFilePath) {
		String resourcePath = PropertiesUtil.class.getResource("").getPath();
		String webInfPath = resourcePath.substring(0,
				resourcePath.lastIndexOf("/classes")).toString();
		cfgFilePath = StringUtils.trimToEmpty(cfgFilePath);
		if (StringUtils.isNotBlank(cfgFilePath)) {
			return webInfPath.concat(cfgFilePath).toString();
		}

		return webInfPath;
	}

	public static void main(String[] args) throws Exception {
		PropertiesUtil propertiesUtil = getInstance();
		if (propertiesUtil == null) {
			System.out.println("pu is null");
		} else {
			System.out.println(propertiesUtil);
		}
		PropertiesUtil propertiesUtil2 = getInstance();
		System.out.println(propertiesUtil2);
	}

	private static class Holder {
		public static final PropertiesUtil instance = new PropertiesUtil();
	}
}
